using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using System.Xml;

namespace DAO
{
    static class OleDbDataAccessHelper
    {
        #region ConnectionString
        protected static String connectionString;
        public static String ConnectionString
        {
            get
            {
                return ReadConnectionString("ConnectionString.xml");
            }
        }
        public static String ReadConnectionString(String file)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(file);
                XmlElement root = doc.DocumentElement;
                String connectionString = root.InnerText;
                return connectionString;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #region ExecuteQuery
        public static DataTable ExecuteQuery(String spName, List<OleDbParameter> OleDbParams)
        {
            DataTable dt = new DataTable();
            try
            {
                OleDbConnection con = new OleDbConnection(ConnectionString);
                con.Open();
                try
                {
                    OleDbCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = spName;
                    if (OleDbParams != null)
                    {
                        foreach (OleDbParameter param in OleDbParams)
                        {
                            cmd.Parameters.Add(param);
                        }
                    }
                    OleDbDataAdapter da = new OleDbDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                }
                catch (OleDbException ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public static DataTable ExecuteQuery(String spName)
        {
            return ExecuteQuery(spName, null);
        }
        #endregion
        #region ExecuteNoneQuery
        public static int ExecuteNoneQuery(String spName, List<OleDbParameter> OleDbParams)
        {
            int n;
            try
            {
                OleDbConnection con = new OleDbConnection(ConnectionString);
                con.Open();
                try
                {
                    OleDbCommand cmd = con.CreateCommand();
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = spName;
                    if (OleDbParams != null)
                    {
                        foreach (OleDbParameter param in OleDbParams)
                        {
                            cmd.Parameters.Add(param);
                        }
                    }
                    n = cmd.ExecuteNonQuery();
                }
                catch (OleDbException ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return n;
        }

        public static int ExecuteNoneQuery(String spName)
        {
            return ExecuteNoneQuery(spName, null);
        }
        #endregion
    }
}